package com.longbuluo;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

import com.longbuluo.util.Configuration;
import com.longbuluo.util.JdbcUtil;

public class Crawler {
	
	private HttpClient client = new HttpClient();
	private DataAccess dataAccess = new DataAccess();
	private List<Movie> movies = new ArrayList<Movie>();
	private String category = "";
	private int totalPage = 1;
	
	public Crawler(String category){
		this.category = category;
	}
	
	public void fetch(){
		try{
			dataAccess.clear(category);
		}catch (Exception e) {
			e.printStackTrace();
		}
		fetchList();
	}
	
	private void fetchList(){
		GetMethod getMethod = null;
		for(int i=1; i<=totalPage; i++){
			try{
				System.out.println(category + ": " + i + "/" + totalPage);
				String url = "http://www.longbuluo.com/tag/" + category + (i>1? "/page/"+ i : ""); 
				getMethod = new GetMethod(url);
				client.executeMethod(getMethod);
				String content = getMethod.getResponseBodyAsString();
				
				Matcher matcher = Pattern.compile("pagetitle([\\s\\S]*?)END CENTER").matcher(content);
				matcher.find();
				content = matcher.group(1);
				matcher = Pattern.compile("id=\"post-(\\d+?)\"").matcher(content);
				while(matcher.find()){
					Movie movie = new Movie();
					movie.setCategory(category);
					movie.setId(Long.parseLong(matcher.group(1)));
					movies.add(movie);
				}

				matcher = Pattern.compile("title=\"第(\\d+?)页\"").matcher(content);
				while(matcher.find()){
					int temp = Integer.parseInt(matcher.group(1));
					if(temp > totalPage){
						totalPage = temp;
					}
				}
				fetchDetail();
				try{
					dataAccess.insert(movies);
				}catch (Exception e) {
				}
				movies.clear();
			}catch (Exception e) {
				e.printStackTrace();
			}finally{
				getMethod.releaseConnection();
			}
		}
	}
	
	private void fetchDetail(){
		GetMethod getMethod = null;
		for (int i = movies.size()-1; i >= 0 ; i--) {
			try{
				Movie movie = movies.get(i);
				getMethod = new GetMethod("http://www.longbuluo.com/movie/" + movie.getId() + ".html");
				client.executeMethod(getMethod);
				String content = getMethod.getResponseBodyAsString();
				
				Matcher matcher = Pattern.compile("class=\"post\"([\\s\\S]*?)<!--END POST-->").matcher(content);
				matcher.find();
				content = matcher.group(1);
				
				matcher = Pattern.compile("(\\d{4}年\\d{2}月\\d{2}日)").matcher(content);
				matcher.find();
				movie.setCreateDate(matcher.group(1));
				
				matcher = Pattern.compile("<h2>([\\s\\S]*?)</h2>").matcher(content);
				matcher.find();
				movie.setTitle(matcher.group(1));
				
				matcher = Pattern.compile("src=\"(http://img.lbldy.com/image.*?)\"").matcher(content);
				if(matcher.find()){
					movie.setPic(matcher.group(1));
				}
				
				StringBuffer sb = new StringBuffer();
				matcher = Pattern.compile("<p><a href=\"(.*?)\".*?>(.*?)</a></p>").matcher(content);
				while(matcher.find()){
					if(sb.length() > 0){
						sb.append("\n");
					}
					sb.append(matcher.group(2));
					sb.append("\n");
					sb.append(matcher.group(1));
				}
				movie.setLinks(sb.toString());
				
				if(sb.length() == 0){
					movies.remove(i);
				}
			}catch (Exception e) {
				e.printStackTrace();
				movies.remove(i);
			}finally{
				getMethod.releaseConnection();
			}
		}
	}
	
//	public static void main(String[] args) {
//		Configuration.load();
//		JdbcUtil.init();
//		
//		Crawler crawler = new Crawler();
//		crawler.fetch();
//	}

}
